//
//  ButtonView.m
//  FastDoctor
//
//  Created by 程建良 on 16/7/7.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "ButtonView.h"



@interface ButtonView()

//画4条线
@property (nonatomic,strong)UIView *lineView1;
@property (nonatomic,strong)UIView *lineView2;
@property (nonatomic,strong)UIView *lineView3;
@property (nonatomic,strong)UIView *lineView4;

//UIButton的数组
@property (nonatomic,strong)NSArray *buttonArray;

@property (nonatomic,strong)NSArray*listCollection;

@property (nonatomic,weak) UILabel *label;

@end
@implementation ButtonView


-(NSArray *)listCollection
{
    if (!_listCollection)
    {
        _listCollection = @[
                            @{@"name":@"肿瘤",@"image":@"zl"},
                            @{@"name":@"心血管",@"image":@"xx"},
                            @{@"name":@"神经科",@"image":@"sj"},
                            @{@"name":@"血液科",@"image":@"xy"},
                            @{@"name":@"骨科",@"image":@"gt"},
                            @{@"name":@"公益",@"image":@"gy"}];
    }
    return _listCollection;
}

-(instancetype)init
{
    if (self = [super init])
    {
        //画几条线
        [self setupLine];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    
    //布局4条线的frame
    self.lineView1.frame = CGRectMake(self.frame.size.width/3, 0, 1, self.frame.size.height);
    self.lineView2.frame = CGRectMake(self.frame.size.width/3*2, 0, 1, self.frame.size.height);
    self.lineView3.frame = CGRectMake(0, self.frame.size.height/2, self.frame.size.width,1 );
    self.lineView4.frame = CGRectMake(0, 0, self.frame.size.width,1 );
    
    
    [self setupButton];
}
-(void)setupLine
{
    self.lineView1 = [[UIView alloc]init];
    self.lineView2 = [[UIView alloc]init];
    self.lineView3 = [[UIView alloc]init];
    self.lineView4 = [[UIView alloc]init];
    
    _lineView1.backgroundColor = [UIColor blackColor];
    _lineView2.backgroundColor = [UIColor blackColor];
    _lineView3.backgroundColor = [UIColor blackColor];
    _lineView4.backgroundColor = [UIColor blackColor];
    
    [self addSubview:_lineView1];
    [self addSubview:_lineView2];
    [self addSubview:_lineView3];
    [self addSubview:_lineView4];
    
}

-(void)setupButton
{
    
    for (int i = 0; i<6; i++)
    {
        UIButton *button = [[UIButton alloc]init];
        
        [self addSubview:button];
        
        button.tag = i;
        
        [button addTarget:self action:@selector(didClickDiseaseButton:) forControlEvents:UIControlEventTouchUpInside];
        
        NSDictionary *dict = self.listCollection[i];
        
        [button setImage:[UIImage imageNamed:dict[@"image"]] forState:UIControlStateNormal];
        
        [button sizeToFit];
        if (kDevice_Is_iPhone5)
        {
            button.frame = CGRectMake(0, 0, 32, 32);
        }
        else if(kDevice_Is_iPhone6Plus)
        {
            button.frame = CGRectMake(0, 0, 42, 42);
        }
            
        
        NSUInteger coloum = i%3;
        NSUInteger line = i/3;
        CGFloat buttonCenterY = self.bounds.size.height/2*line +self.bounds.size.height/4;
        CGFloat buttonCenterX = self.bounds.size.width/3*coloum+self.bounds.size.width/6;
        
        button.center = CGPointMake(buttonCenterX, buttonCenterY);

        //创建label
        UILabel *label = [[UILabel alloc] init];
        self.label = label;
        
        [self addSubview:label];
        
        label.text = dict[@"name"];
        
        
        if (kDevice_Is_iPhone5)
        {
            [label setFont:[UIFont systemFontOfSize:12]];
        }
        else if(kDevice_Is_iPhone6Plus)
        {
            [label setFont:[UIFont systemFontOfSize:22]];
        }
        else
        {
             [label setFont:[UIFont systemFontOfSize:16]];
        }
        
        label.textAlignment = NSTextAlignmentCenter;
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.mas_equalTo(button.mas_bottom).offset(5);
            make.centerX.mas_equalTo(button);
            
        }];
    }
}

//点击button的点击事件
- (void)didClickDiseaseButton:(ButtonView *)sender {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"disease" object:nil userInfo:@{@"button":sender}];
}


@end
